var ModuleUtil = {
  $alert: function (msg, title) {
    if (title === undefined) {
      title = '信息';
    }

    if (window.Vue === undefined) {
      window.alert(msg);
      return;
    }
    Vue.prototype.$alert(msg, title);
  },

  getDeptWorkshop: function (deptWork) {
    var deptId = '',
      workShopId = '',
      pickingPeople = '';
    for (var i = 0; i < deptWork.length; i++) {
      var dataId = deptWork[i];
      dataId = dataId.split('_');
      var type = dataId[0],
        id = dataId[1];
      if (type === 'dept') {
        deptId = id;
      } else if (type === 'workshop') {
        workShopId = id;
      } else if (type === 'user') {
        pickingPeople = id;
      }
    }

    return {
      deptId: deptId,
      workShopId: workShopId,
      pickingPeople: pickingPeople
    };
  },

  checkIssueValid: function (type, checkData, materialData, saveData) {
    if (checkData.length === 0) {
      this.$alert('请选择需要发料的物料');
      return false;
    }

    if (type === 'workshop') {
      var deptAndWork = saveData.deptAndWork;
      if (deptAndWork.length === 0) {
        this.$alert('请选择发料给车间的人员');
        return false;
      }
      var dataList = _.uniqBy(checkData, function (data) {
        if (data.orderId === null) {
          return 0;
        }
        return 1;
      });
      if (dataList.length > 1) {
        this.$alert('投料单与物料不能一起发料');
        return false;
      }
    } else {
      var obj = _.find(checkData, function (issue) {
        return !SysUtil.isEmpty(issue.orderId);
      });
      if (obj === undefined) {
        this.$alert('只有投料单的物料才能发送到外协');
        return false;
      }
    }

    var saveDataGroup = _.groupBy(checkData, 'materialCode');
    for (var materialCode in saveDataGroup) {
      var materialCodeList = saveDataGroup[materialCode];
      var totalCount = _.sumBy(materialCodeList, 'receiveCount');
      totalCount = _.round(totalCount, 4);
      if (totalCount > materialData[materialCode].count) {
        this.$alert('库存不足');
        return false;
      }

      var totalProductionCount = _.sumBy(materialCodeList, 'productionUnitReceiveCount');
      if (totalProductionCount > materialData[materialCode].productionUnitCount) {
        this.$alert('生产单位库存不足');
        return false;
      }
    }
    return true;
  },

  checkIssueRowValid: function (issue) {
    var orderId = issue.orderId;
    var receiveCount = issue.receiveCount;
    if (SysUtil.isEmpty(orderId)) {
      if (receiveCount === undefined) {
        this.$alert('请输入发料数', '信息');
        return false;
      }
    } else {
      var productionUnitReceiveCount = issue.productionUnitReceiveCount;
      if (productionUnitReceiveCount === null) {
        if (receiveCount === undefined || receiveCount === 0) {
          this.$alert('请输入发料数', '信息');
          return false;
        }
      } else if (receiveCount === null) {
        if (productionUnitReceiveCount === undefined || productionUnitReceiveCount === 0) {
          this.$alert('请输入生产单位发料数', '信息');
          return false;
        }
      } else {
        if ((receiveCount === undefined || receiveCount === 0) &&
          (productionUnitReceiveCount === undefined || productionUnitReceiveCount === 0)) {
          this.$alert('发料数与生产单位发料数不能同时为空', '信息');
          return false;
        }
      }

      if (issue.selfOutFuCount !== null) {
        if (SysUtil.isEmpty(issue.selfOutFuCount)) {
          this.$alert("请输入交接数量", "信息");
          return false;
        }
      }
    }
    return true;
  },

  getIssuePostData: function (data, type, saveData) {
    if (type === undefined) {
      type = 'workshop';
    }

    var unitPrice = data.unitPrice;
    var receiveCount = data.receiveCount;
    var sumPrice = unitPrice * receiveCount;

    var postDataList = [];
    var postData = {
      sourceType: 1,
      warehouseType: 4,
      outCountPrice: unitPrice,
      inOutMoney: _.round(sumPrice, 4)
    };

    if (type === 'workshop') {
      var deptAndWork = saveData.deptAndWork;
      deptAndWork = this.getDeptWorkshop(deptAndWork);
      postData.deptId = deptAndWork.deptId;
      postData.workShopId = deptAndWork.workShopId;
      postData.pickingPeople = deptAndWork.pickingPeople;
    }

    var fields = ['materialCode', 'materialName', 'materialType', 'providerName', 'model', 'color',
      'remark'
    ];
    if (SysUtil.isEmpty(data.orderId)) {
      postData.count = data.receiveCount;
      postData.type = 5;
      postData.isProductionUnit = 0;
      SysUtil.setPostData(postData, data, fields, false, true);
      postDataList.push(postData);
    } else {
      postData.type = 2;
      postData.feedOrder = data.id;
      postData.orderId = data.orderId;
      postData.rowIndex = data.lineNo;

      if (type !== 'workshop') {
        postData.source = 2;
      }

      SysUtil.setPostData(postData, data, fields, false, true);
      var receiveCount = data.receiveCount;
      if (!SysUtil.isEmpty(receiveCount) && receiveCount !== 0) {
        var obj = _.cloneDeep(postData);
        obj.count = receiveCount;
        obj.isProductionUnit = 0;
        var selfOutFuCount = data.selfOutFuCount;
        obj.selfOutFuCount = selfOutFuCount === null ? 0 : selfOutFuCount;
        postDataList.push(obj);
      }

      var productionUnitReceiveCount = data.productionUnitReceiveCount;
      if (!SysUtil.isEmpty(productionUnitReceiveCount) && productionUnitReceiveCount !== 0) {
        var obj = _.cloneDeep(postData);
        obj.count = productionUnitReceiveCount;
        obj.isProductionUnit = 1;
        postDataList.push(obj);
      }
    }

    return postDataList;
  },

  getIssueOutOrderPostData: function (order) {
    var outOrder = order.outOrder;
    var supplier = outOrder.supplier;
    var technology = outOrder.technology;

    var postData = {
      orderId: order.orderId,
      materialCode: order.materialCode,
      supplierId: supplier.id,
      supplierName: supplier.supplierName,
      technology: technology.technology,
      flowCycle: outOrder.flowCycle,
      maching: order.materialType,
      color: order.colorDescription,
      count: order.selfOutFuCount,
      valuationUint: outOrder.valuationUint,
      valuationCount: outOrder.valuationCount,
      unitPrice: outOrder.unitPrice,
      type: 0,
      rowIndex: order.lineNo,
      remark: order.remark,
      additionalMaterialType: '',
      additionalRowIndex: ''
    };

    if (outOrder.hasEle) {
      var electroplate = outOrder.electroplate;
      postData.additionalMaterialType = electroplate.materialType;
      postData.additionalRowIndex = electroplate.lineNo;
      postData.typeId = electroplate.lineNo;
    }

    return postData;
  },

  getEleOrderData: function (orderId) {
    return ajaxRequest('putOutNew/getDianDuColorList', {
      orderId: orderId
    }).then(function (res) {
      var eleColorList = res.dianDuColorList;
      var eleColors = [];
      for (var i = 0; i < eleColorList.length; i++) {
        var eleColor = eleColorList[i];
        var colorDescription = eleColor.colorDescription;
        colorDescription = colorDescription.trim();
        if (colorDescription === '') {
          continue;
        }

        var orderTotal = eleColor.orderTotal;
        if (SysUtil.isEmpty(orderTotal)) {
          orderTotal = undefined;
        } else {
          orderTotal = Number(orderTotal);
        }
        eleColors.push({
          lineNo: eleColor.lineNo,
          materialType: eleColor.materialType,
          count: orderTotal,
          color: colorDescription
        });
      }
      var eleColorObj = _.groupBy(eleColors, 'materialType');

      var existMaterialList = res.aleradyPutOutMaterialList;
      var material = {};
      for (var i = 0; i < existMaterialList.length; i++) {
        var existMaterial = existMaterialList[i];
        var additionalMaterialType = existMaterial.additionalMaterialType;
        if (additionalMaterialType !== '') {
          material[existMaterial.materialType] = additionalMaterialType;
        }
      }

      return {
        eleColorObj: eleColorObj,
        material: material
      };
    });
  },

  getEleOrderSelect: function (order, eleDataObj) {
    var option = {
      hasEle: false,
      canEle: false
    };

    if (order.lineNo !== '-1') {
      return option;
    }

    var orderId = order.orderId;
    var eleData = eleDataObj[orderId];
    if (!eleData) {
      return option;
    }

    var material = eleData.material;
    if (SysUtil.isEmpty(material)) {
      return option;
    }

    var elec = material[order.materialType];
    if (elec === undefined) {
      option.canEle = true;
    } else {
      option.hasEle = true;
    }

    return option;
  }
};
